package com.zr.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zr.dao.FindTeaDaoljc;
import com.zr.model.Teacher;
import com.zr.util.JDBCUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class FindTeaDaoImplljc implements FindTeaDaoljc{

	@Override
	public JSONArray getAllTeacher() {
		JSONArray j = new JSONArray();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT teacher.tname,teacher.tid FROM teacher");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				JSONObject j1 = new JSONObject();
				j1.put("id", rs.getInt("tid"));
				j1.put("text", rs.getString("tname"));
				j.add(j1);	
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return j;
	}
	//教师总数
	@Override
	public int countAllTeacher() {
		int count = 0;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT COUNT(teacher.tid) as count ");
		sql.append(" FROM teacher");
		Connection con = JDBCUtil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				count =Integer.parseInt(rs.getString("count"));
			}	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

}
